package com.example.java_chatroom_redis_rabbitmq.mapper;

import com.example.java_chatroom_redis_rabbitmq.model.Session;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SessionMapper {
    @Select("select sessionId,friendId from session_user where userId=#{userId}")
    List<Session> getSessionListByUserId(@Param("userId") Integer userId);

    @Select("select lastMessage,lastTime from session where sessionId=#{sessionId}")
    Session getSessionInfoBySessionId(@Param("sessionId") Integer sessionId);

    @Options(useGeneratedKeys = true,keyProperty = "sessionId")
    @Insert("insert into session values(null,'',now())")
    Integer createSession(Session session);

    @Insert("insert into session_user(sessionId,userId,friendId) values(#{sessionId},#{userId},#{friendId}),(#{sessionId},#{friendId},#{userId})")
    Integer createSessionUser(@Param("sessionId") Integer sessionId,@Param("userId") Integer userId,@Param("friendId") Integer friendId);

    @Select("select userId from session_user where sessionId=#{sessionId} and userId!=#{userId}")
    List<Integer> getUserIdsBySessionId(@Param("sessionId") Integer sessionId,@Param("userId") Integer userId);

    @Update("update session set lastMessage=#{lastMessage},lastTime=now() where sessionId=#{sessionId}")
    Integer updateSession(@Param("sessionId") Integer sessionId,@Param("lastMessage") String lastMessage);
}
